<?php 
require_once dirname(__FILE__) . '/__cdb.conf.php';

//
$data_using = db2data($db_using);
$data_reference = db2data($db_reference);

//__dump($data_using);

$miss_table = get_miss_table($data_reference, $data_using);
$miss_field = get_miss_field($data_reference, $data_using);
$diff_type = get_diff_type($data_reference, $data_using);

//Compare 2 data, find miss table
function get_miss_table($data_reference, $data_using) {
  $data = new stdClass();
  foreach ($data_reference as $ref_t => &$ref_c) {
    if (!isset($data_using->$ref_t)) {
      $data->$ref_t = $ref_c;
    }
  }
  return $data;
}
//Compare 2 data, find miss field for the same table
function get_miss_field($data_reference, $data_using) {
  $data = new stdClass();
  foreach ($data_reference as $ref_t => &$ref_c) {
    if (!isset($data_using->$ref_t)) {
      continue;
    }
    $data->$ref_t = new stdClass();
    foreach ($data_reference->$ref_t as $ref_df => &$ref_tp) {
      if (!isset($data_using->$ref_t->$ref_df)) {
        $data->$ref_t->$ref_df = $ref_tp;
      }
    }
    if (count(get_object_vars($data->$ref_t)) == 0) {
      unset($data->$ref_t);
    }
  }
  return $data;
}
//Compare 2 data, find different type for the same table and the same field
function get_diff_type($data_reference, $data_using) {
  $data = new stdClass();
  foreach ($data_reference as $ref_t => &$ref_c) {
    if (!isset($data_using->$ref_t)) {
      continue;
    }
    $data->$ref_t = new stdClass();
    foreach ($data_reference->$ref_t as $ref_df => &$ref_tp) {
      if (isset($data_using->$ref_t->$ref_df) && 
           ($data_using->$ref_t->$ref_df->Type != $data_reference->$ref_t->$ref_df->Type || 
            $data_using->$ref_t->$ref_df->Null != $data_reference->$ref_t->$ref_df->Null ||
            $data_using->$ref_t->$ref_df->Key != $data_reference->$ref_t->$ref_df->Key ||
            $data_using->$ref_t->$ref_df->Default != $data_reference->$ref_t->$ref_df->Default ||
            $data_using->$ref_t->$ref_df->Extra != $data_reference->$ref_t->$ref_df->Extra)) {
        $data->$ref_t->$ref_df = $ref_tp;
      }
    }
    if (count(get_object_vars($data->$ref_t)) == 0) {
      unset($data->$ref_t);
    }
  }
  return $data;
}
//db2data, data is like a 2D array contains tables and their fiels (with type info)
function db2data($db) {
  global $host, $login, $pass;
  try {
    $myPdo = new PDO("mysql:host=$host", $login, $pass);
    $myPdo->exec("USE $db");
    $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  } catch (PDOException $e) {
  }
  $attr = "Tables_in_$db";
  $tstmt = $myPdo->prepare('SHOW TABLES');
  $tstmt->execute();
  $data = new stdClass();
  $i = 0;
  while ($i < $tstmt->rowCount()) {
    $tname = $tstmt->fetch(PDO::FETCH_OBJ)->$attr;
    $cstmt = $myPdo->prepare("SHOW FIELDS FROM $tname");
    $cstmt->execute();
    $tobj = new stdClass();
    $j = 0;
    while ($j < $cstmt->rowCount()) {
      $fobj = $cstmt->fetch(PDO::FETCH_OBJ);
      $tmp = $fobj->Field;
      unset($fobj->Field);
      $tobj->$tmp = $fobj;
      $j++;
    }
    $data->$tname = $tobj;
    $i++;
  }
  $myPdo = NULL;
  return $data;
}
?>

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  outline: 0;
  padding: 0;
  margin: 0;
}
body {
  margin: 5px;
}
h2 span {
  background: #ddd;
  border: 1px solid #aaa;
  color: #666;
  padding: 0 10px;
}
table.list {
  font-size: 12px;
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
}
table.list td {
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  padding: 0 5px;
}
table.list td.title {
  font-weight: bold;
  min-width: 350px;
}
table.list tr.odd td {
  background: #fff;
}
table.list tr.even td {
  background: #eee;
}
</style>
</head>
<body>
<h2>Current Table: <span><?php echo $db_using; ?></span> Reference Table: <span><?php echo $db_reference; ?></span></h2>
<div>&nbsp;</div>
<h3>Miss Tables</h3>
<table id="miss_table" class="list" cellspacing=0 cellpadding=0></table>
<div>&nbsp;</div>
<h3>Miss Fields</h3>
<table id="miss_field" class="list" cellspacing=0 cellpadding=0></table>
<div>&nbsp;</div>
<h3>Diff Fields</h3>
<table id="diff_type" class="list" cellspacing=0 cellpadding=0></table>
<div>&nbsp;</div>
<script type="text/javascript" src="__cdb_jq.js"></script>
<script type="text/javascript" src="__cdb_json.js"></script>
<script type="text/javascript">
$(function(){
  var miss_table = JSON.parse('<?php echo addslashes(json_encode($miss_table)); ?>');
  var miss_field = JSON.parse('<?php echo addslashes(json_encode($miss_field)); ?>');
  var diff_type = JSON.parse('<?php echo addslashes(json_encode($diff_type)); ?>');
  set('miss_table', miss_table);
  set('miss_field', miss_field);
  set('diff_type', diff_type);
});

function set(table_id, data) {
  var btn_txt = 'Create Table';
  if (table_id == 'miss_field') {
    btn_txt = 'Add Field';
  } else if (table_id == 'diff_type') {
    btn_txt = 'Alter Type';
  }
  
  var table, tr, td, elm;
  table = $('#' + table_id)[0];
  var count = 0;
  for (var idx in data) {
    var obj = data[idx];
    tr = table.insertRow(-1);;
    tr.className = (count % 2 == 0) ? 'even' : 'odd';
    td = tr.insertCell(-1);
    td.className = 'title';
    td.appendChild(document.createTextNode(idx));
    td = tr.insertCell(-1);
    elm = document.createElement('button');
    elm._idx = idx;
    elm._obj = obj;
    elm.onclick = function() {
      var r = window.confirm('CLICK OK TO PROCEED');
      if (r) {
        $.ajax({
          type: 'POST',
          url: '__cdb_server.php',
          data: 'action=' + table_id + '&table=' + this._idx + '&content=' + encodeURIComponent(JSON.stringify(this._obj)),
          success: function(msg){
            location.reload();
          }
        });
      }
    };
    elm.appendChild(document.createTextNode(btn_txt));
    td.appendChild(elm);
    for (var idx2 in obj) {
      var obj2 = obj[idx2];
      tr = table.insertRow(-1);
      tr.className = (count % 2 == 0) ? 'even' : 'odd';
      td = tr.insertCell(-1);
      td.appendChild(document.createTextNode(idx2));
      td = tr.insertCell(-1);
      td.style.width = '250px';
      td.appendChild(document.createTextNode(obj2str(obj2)));
    }
    count++;
  }
  if (count == 0) {
    tr = table.insertRow(-1);;
    tr.className = 'even';
    td = tr.insertCell(-1);
    td.className = 'title';
    td.appendChild(document.createTextNode('-'));
    td = tr.insertCell(-1);
    td.style.width = '250px';
  }
};

function obj2str(obj) {
  var result = '';
  for (var atrr in obj) {
    var val = obj[atrr];
    if(result != '') {
      result += ', ';
    }
    result += (val == '') ? '-' : val;
  }
  return result;
}
</script>
</body>
</html>